package com.apchernykh.types;

import java.util.LinkedList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class StationListDBHelper extends
		android.database.sqlite.SQLiteOpenHelper {

	public StationListDBHelper(Context iContext) {
		super(iContext, DATABASE_NAME, null, DATABASE_VERSION);
	}

	public StationListDBHelper(Context iContext, String iName,
			CursorFactory iFactory, int iDbVersion) {
		super(iContext, iName, iFactory, iDbVersion);
	}


	private static final String NAME = "NAME";
	private static final String LOW_BR_URL = "LOWBRURL";
	private static final String HI_BR_URL = "HIBRURL";
	private static final String DESCRIPTION = "DESC";
	private static final String CATEGORY = "CAT";
	private static final String PLAYING_NOW_URL = "PLAYING_NOW";
	private static final String PLAYING_NEXT_URL = "PLAYING_NEXT";
	private static final String ID = "ID";
	private static final String[] ALL_COLUMNS = { NAME, LOW_BR_URL, HI_BR_URL,
			DESCRIPTION, CATEGORY, PLAYING_NOW_URL, PLAYING_NEXT_URL, ID };
	private static final int DATABASE_VERSION = 2;
	private static final String DATABASE_NAME = "ROZHLAS";
	private static final String STATIONSLIST_TABLE_NAME = "stations";
	private static final String STATIONSLIST_TABLE_CREATE = "CREATE TABLE "
			+ STATIONSLIST_TABLE_NAME + " (" + NAME + " TEXT, " + LOW_BR_URL
			+ " TEXT, " + HI_BR_URL + " TEXT, " + DESCRIPTION + " TEXT, "
			+ CATEGORY + " TEXT, " + ID + " INT, " + PLAYING_NOW_URL + " TEXT,"
			+ PLAYING_NEXT_URL + " TEXT" + ");";

    private static final String STATIONSLIST_TABLE_DELETE =
            "DROP TABLE IF EXISTS " + STATIONSLIST_TABLE_NAME +";";

	
	@Override
	public void onCreate(SQLiteDatabase iDb) {
		iDb.execSQL(STATIONSLIST_TABLE_DELETE);
		iDb.execSQL(STATIONSLIST_TABLE_CREATE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase iDb, int iOldVersion, int iNewVersion) {
		onCreate(iDb);
	}

	@Override
	public void onDowngrade(SQLiteDatabase iDb, int iOldVersion, int iNewVersion) {
		onCreate(iDb);
	}

	public void clear() {
		getWritableDatabase().delete(STATIONSLIST_TABLE_NAME, null, null);
		close();
	}

	public void addStation(StationInfo iStation, int iId) {
		ContentValues aValues = new ContentValues();
		aValues.put(NAME, iStation.mShortName);
		aValues.put(LOW_BR_URL, iStation.mp3URL.getURL("low"));
		aValues.put(HI_BR_URL, iStation.mp3URL.getURL("hi"));
		aValues.put(DESCRIPTION, iStation.mShortdescription);
		aValues.put(CATEGORY, iStation.mCategory);
		aValues.put(PLAYING_NOW_URL, iStation.mOnGoing.mNow);
		aValues.put(PLAYING_NEXT_URL, iStation.mOnGoing.mNext);
		aValues.put(ID, iId);
		getWritableDatabase().insert(STATIONSLIST_TABLE_NAME, null, aValues);
	}

	public List<StationInfo> getStationsList() {
		LinkedList<StationInfo> mStations = new LinkedList<StationInfo>();
		Cursor aResult = getReadableDatabase().query(STATIONSLIST_TABLE_NAME,
				ALL_COLUMNS, null, null, null, null, ID);
		if (aResult.getCount() > 0) {
			while (aResult.moveToNext()) {
				String aName = aResult.getString(aResult.getColumnIndex(NAME));
				String aLowBitrateUrl = aResult.getString(aResult
						.getColumnIndex(LOW_BR_URL));
				String aHightBitrateUrl = aResult.getString(aResult
						.getColumnIndex(HI_BR_URL));
				String aDescription = aResult.getString(aResult
						.getColumnIndex(DESCRIPTION));
				String aCategory = aResult.getString(aResult
						.getColumnIndex(CATEGORY));
				StationInfo aStation = new StationInfo(aName, aLowBitrateUrl,
						aHightBitrateUrl);
				aStation.mOnGoing = new OnGoingInfoURL(aResult.getString(aResult
						.getColumnIndex(PLAYING_NOW_URL)),
						aResult.getString(aResult
								.getColumnIndex(PLAYING_NEXT_URL)));
				aStation.mShortdescription = aDescription;
				aStation.mCategory = aCategory;
				mStations.add(aStation);
			}
		}
		aResult.close();
		close();
		return mStations;
	}

	public void reset(List<StationInfo> aStationsList) {
		clear();
		for (StationInfo aCurrentStation : aStationsList) {
			addStation(aCurrentStation, aCurrentStation.mStationId);
		}
		close();
	}
}
